package com.ihr360.insurance.model.dto.benefits;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ihr360.commons.annotation.CodeGenerator;
import com.ihr360.insurance.model.vo.BenefitTypeVo;
import com.ihr360.shared.entitywithlistener.NumberIdWithTime;
import com.ihr360.shared.sequence.handler.CodeGeneratorHandler;
import org.hibernate.annotations.NaturalId;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * 法定福利模板
 */
@Entity
@Table
@EntityListeners(CodeGeneratorHandler.class)
public class MandatoryBenefitTemplate extends NumberIdWithTime<Long> {

    private static final long serialVersionUID = -2118840430411535899L;

    /**
     * 代码
     */
    @CodeGenerator(bizModel = "mandatory_benefit_template", serialTmpl = "SERIALGEN(MAXID)", name = "社保福利模板序列号", categoryId = "SERIALID(\"s\")")
    @Column(length = 32, nullable = false)
    private String code;

    /**
     * 规则
     */
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "template")
    private List<PaymentConfig> configs = new ArrayList<>();

    /**
     * 社保分组
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @NaturalId(mutable = true)
    private MandatoryBenefitGroup group;

    /**
     * 法定福利类别(险种)
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @NaturalId(mutable = true)
    private MandatoryBenefitsType type;

    @JsonIgnore
    public BenefitTypeVo getVoType() {
        if (type == null) {
            return null;
        }
        return new BenefitTypeVo(type.getId(), type.getCode(), type.getName(), type.getCategory(),
                type.getEffectiveAt(), type.getInvalidAt(), type.isEnabled());
    }

    public MandatoryBenefitGroup getGroup() {
        return group;
    }

    public void setGroup(MandatoryBenefitGroup group) {
        this.group = group;
    }

    public MandatoryBenefitsType getType() {
        return type;
    }

    public void setType(MandatoryBenefitsType type) {
        this.type = type;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<PaymentConfig> getConfigs() {
        return configs;
    }

    public void setConfigs(List<PaymentConfig> configs) {
        this.configs.clear();
        this.configs.addAll(configs);
    }
}